﻿using CTM.Web.Data;
using CTM.Web.Models.Input;
using CTM.Web.Repositories;
using CTM.Web.Utils;
using System;
using System.Collections.Generic;

namespace CTM.Web.Services
{
    public interface INotificationService : IApplicationService<Notification>
    {
        IEnumerable<Notification> GetByUser(string userId);
        void Create(object entity, string currentUserId);
    }

    public class NotificationService : ApplicationService<Notification>, INotificationService
    {
        private IUnitOfWork unitOfWork;
        private INotificationRepository notificationRepository;
        private IAccountRepository accountRepository;

        public NotificationService(IUnitOfWork unitOfWork, INotificationRepository notificationRepository, IAccountRepository accountRepository)
            : base(unitOfWork, notificationRepository)
        {
            this.unitOfWork = unitOfWork;
            this.notificationRepository = notificationRepository;
            this.accountRepository = accountRepository;
        }

        public IEnumerable<Notification> GetByUser(string userId)
        {
            return notificationRepository.Get(n => !n.IsDeleted && n.Receiver.Id == userId);
        }

        public void Create(object entity, string currentUserId)
        {
            var currentUser = accountRepository.GetById(currentUserId);
            if (currentUser == null) return;

            var entityType = entity.GetType().FullName;
            Post post = null;
            Comment comment = null;
            NotificationType type = NotificationType.Other;

            switch (entityType)
            {
                case "CTM.Web.Models.Input.Post":
                    post = (Post)entity;
                    type = NotificationType.Post;
                    break;

                case "CTM.Web.Models.Input.Comment":
                    comment = (Comment)entity;
                    post = comment.Post;
                    type = NotificationType.Comment;
                    break;
            }

            // send to manager
            if (currentUserId != post.Manager.Id)
            {
                var notification = new Notification
                {
                    Id = Guid.NewGuid(),
                    Type = type,
                    IsDeleted = false,
                    CreatedOn = DateTime.UtcNow,
                    CreatedBy = currentUserId,
                    Giver = currentUser,
                    Receiver = accountRepository.GetById(post.Manager.Id),
                };
                switch (entityType)
                {
                    case "CTM.Web.Models.Input.Post":
                        notification.Post = post;
                        break;
                    case "CTM.Web.Models.Input.Comment":
                        notification.Comment = comment;
                        break;
                }
                this.Insert(notification);
            }

            // send to employee
            if (currentUserId != post.Employee.Id)
            {
                var notification = new Notification
                {
                    Id = Guid.NewGuid(),
                    Type = type,
                    IsDeleted = false,
                    CreatedOn = DateTime.UtcNow,
                    CreatedBy = currentUserId,
                    Giver = currentUser,
                    Receiver = accountRepository.GetById(post.Employee.Id),
                };
                switch (entityType)
                {
                    case "CTM.Web.Models.Input.Post":
                        notification.Post = post;
                        break;
                    case "CTM.Web.Models.Input.Comment":
                        notification.Comment = comment;
                        break;
                }
                this.Insert(notification);
            }
        }
    }
}